﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;

namespace NMA.Web.Tests.Helper
{
    using NMA.Web.Areas.Admin.Models;
    using NMA.Web.Core.Helper;

    [TestFixture]
    public class PagingHelperTests
    {
        [Test]
        public void Can_valid_calculating()
        {
            var pager = PagingModelFactory.CreatePagingModel1();
            Assert.AreEqual(pager.PagesToDisplay, 10, "PagesToDisplay");
            Assert.AreEqual(pager.TotalPages, 10, "TotalPages");
            Assert.AreEqual(pager.HasPreviousPage, false, "HasPreviousPage");
            Assert.AreEqual(pager.HasNextPage, true, "HasNextPage");
            Assert.AreEqual(pager.MinItemIndexOfCurrentPage, 1, "MinItemIndexOfCurrentPage");
            Assert.AreEqual(pager.MaxItemIndexOfCurrentPage, 10, "MaxItemIndexOfCurrentPage");
            Assert.AreEqual(pager.MaxPageIndexToDisplay, 10, "MaxPageIndexToDisplay");
            Assert.AreEqual(pager.MinPageIndexForDisplaying, 1, "MinPageIndexForDisplaying");

            pager = PagingModelFactory.CreatePagingModel2();
            Assert.AreEqual(pager.PagesToDisplay, 2, "PagesToDisplay");
            Assert.AreEqual(pager.TotalPages, 2, "TotalPages");
            Assert.AreEqual(pager.HasPreviousPage, true, "HasPreviousPage");
            Assert.AreEqual(pager.HasNextPage, false, "HasNextPage");
            Assert.AreEqual(pager.MinItemIndexOfCurrentPage, 11, "MinItemIndexOfCurrentPage");
            Assert.AreEqual(pager.MaxItemIndexOfCurrentPage, 11, "MaxItemIndexOfCurrentPage");
            Assert.AreEqual(pager.MaxPageIndexToDisplay, 2, "MaxPageIndexToDisplay");
            Assert.AreEqual(pager.MinPageIndexForDisplaying, 1, "MinPageIndexForDisplaying");
        }

        [Test]
        public void Can_valid_rendering()
        { 
            var pager = PagingModelFactory.CreatePagingModel1();
            var result = PagingHelper.RenderingPager(null, pager);

            Assert.IsTrue(result.Contains("<li class=\"previous\">Previous</li>"));
            Assert.IsTrue(result.Contains(string.Format("<li class=\"next\"><a href=\"{0}\" class=\"page{1}\">Next</a></li>", pager.PageActionLink.Replace("{page}", (pager.PageIndex + 1).ToString()), pager.PageIndex + 1)));
        }
    }

    public static class PagingModelFactory
    {
        public static PagingModel CreatePagingModel1()
        {
            return new PagingModel(1, 10, 100, "", "");
        }

        public static PagingModel CreatePagingModel2()
        {
            return new PagingModel(2, 10, 11, "", "");
        }
    }
}
